<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
      <title>NC優化 | HiAPI-C# 2025 </title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta name="title" content="NC優化 | HiAPI-C# 2025 ">
      
      
      <link rel="icon" href="../../../../../img/HiAPI.favicon.ico">
      <link rel="stylesheet" href="../../../../../public/docfx.min.css">
      <link rel="stylesheet" href="../../../../../public/main.css">
      <meta name="docfx:navrel" content="../../../../../toc.html">
      <meta name="docfx:tocrel" content="../../toc.html">
      
      <meta name="docfx:rel" content="../../../../../">
      
      
      
      <meta name="loc:inThisArticle" content="In this article">
      <meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
      <meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
      <meta name="loc:tocFilter" content="Filter by title">
      <meta name="loc:nextArticle" content="Next">
      <meta name="loc:prevArticle" content="Previous">
      <meta name="loc:themeLight" content="Light">
      <meta name="loc:themeDark" content="Dark">
      <meta name="loc:themeAuto" content="Auto">
      <meta name="loc:changeTheme" content="Change theme">
      <meta name="loc:copy" content="Copy">
      <meta name="loc:downloadPdf" content="Download PDF">

      <script type="module" src="./../../../../../public/docfx.min.js"></script>

      <script>
        const theme = localStorage.getItem('theme') || 'auto'
        document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
      </script>

  </head>

  <body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
    <header class="bg-body border-bottom">
      <nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
        <div class="container-xxl flex-nowrap">
          <a class="navbar-brand" href="../../../../../index.html">
            <img id="logo" class="svg" src="../../../../../img/HiAPI.logo.png" alt="">
            
          </a>
          <button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
            <i class="bi bi-three-dots"></i>
          </button>
          <div class="collapse navbar-collapse" id="navpanel">
            <div id="navbar">
              <form class="search" role="search" id="search">
                <i class="bi bi-search"></i>
                <input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
              </form>
            </div>
          </div>
        </div>
      </nav>
    </header>

    <main class="container-xxl">
      <div class="toc-offcanvas">
        <div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
          <div class="offcanvas-header">
            <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
            <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
          </div>
          <div class="offcanvas-body">
            <nav class="toc" id="toc"></nav>
          </div>
        </div>
      </div>

      <div class="content">
        <div class="actionbar">
          <button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
            <i class="bi bi-list"></i>
          </button>

          <nav id="breadcrumb"></nav>
        </div>

        <article data-uid="Cmd-NcOptimization">
          <h1 id="nc優化">NC優化</h1>

<p>組態在NC仿真前設定，然後執行<strong>輸出優化NC檔</strong>指令。</p>
<p>可同時參考閱讀說明章節<a class="xref" href="../../optimization/index.html">NC優化</a>。</p>
<h2 id="組態">組態</h2>
<div class="NOTE">
<h5>Note</h5>
<p><strong>指令格式</strong></p>
<pre><code class="lang-csharp">OptEnableFeedrate = &lt;布林值&gt;;
OptEnableInterpolation = &lt;布林值&gt;;
OptRapidFeed_mmdmin = &lt;實數&gt;;
OptMinFeedrate_mmdmin = &lt;實數&gt;;
OptMaxFeedrate_mmdmin = &lt;實數&gt;;
OptMaxAcceleration_mmds2 = &lt;實數&gt;;
OptExtendedPreDistance_mm = &lt;實數&gt;;
OptExtendedPostDistance_mm = &lt;實數&gt;;
OptSpindlePowerSafetyFactor = &lt;實數&gt;;
OptSpindleTorqueSafetyFactor = &lt;實數&gt;;
OptThermalYieldSafetyFactor = &lt;實數&gt;;
OptPreferedForce_N = &lt;實數&gt;;
</code></pre>
</div>
<ul>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableFeedrate">OptEnableFeedrate</a><small>(API)</small>：啟用順序式(Sequential)進給率優化。</p>
<p>順序式係指該功能從下指令開始作用。與其相對的設定在刀具端，刀具端也能設定進給率優化，但是在裝刀時才會發揮作用。</p>
<p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableFeedrate">OptEnableFeedrate</a><small>(API)</small>與所有其他啟用進給率優化的指令需同時為真，進給率優化才會啟用。
即與刀具設定端必須同時為真(true)，進給率優化才會啟用。</p>
<p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableFeedrate">OptEnableFeedrate</a><small>(API)</small>預設值為真。</p>
</li>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableInterpolation">OptEnableInterpolation</a><small>(API)</small>：優化程序用重新插補。</p>
</li>
</ul>
<p>重新插捕後NC碼會增加，可以更好地控制加減速。</p>
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptFeedrateAssignmentRatio">OptFeedrateAssignmentRatio</a><small>(API)</small>：進給賦值倍率。</li>
</ul>
<p>當優化程序用重新插補並且進給率變化超過進給賦值倍率時，系統會依據<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxAcceleration_mmds2">OptMaxAcceleration_mmds2</a><small>(API)</small>插入附帶進給率指令的NC碼。</p>
<p>進給賦值倍率越小，插補點越密集。進給賦值倍率越大，則偏離優化標的的程度越大。</p>
<p>在改進給賦值倍率的同時也要對應加大安全係數。
進給賦值倍率若是0.01，就是1%，每變更1%進給率就會給個新的插補點。
如果改大，比方說10%，可能會變成該處理論值應該是F100，可是優化後卻是F110，所以要配合安全係數修改。</p>
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptRapidFeed_mmdmin">OptRapidFeed_mmdmin</a><small>(API)</small>：優化程序中的無切削區域進給率。單位mm/min。</li>
</ul>
<p>沒有切到工件的NC路徑，為無切削區域。優化後該路徑將採用無切削區域進給率。</p>
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMinFeedrate_mmdmin">OptMinFeedrate_mmdmin</a><small>(API)</small>：優化程序中的有切削區域最小進給。單位mm/min。</li>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxFeedrate_mmdmin">OptMaxFeedrate_mmdmin</a><small>(API)</small>：優化程序中的有切削區域最大進給。單位mm/min。</li>
</ul>
<p>有切到工件的NC路徑，為有切削區域。有切削區域的最大最小進給的設定通常依據經驗法則。</p>
<ul>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxAcceleration_mmds2">OptMaxAcceleration_mmds2</a><small>(API)</small>：優化程序中的加減速限制。優化後NC碼的F值變化在該限制之內。單位 <span class="math">\(mm/s^2\)</span>。</p>
</li>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPreDistance_mm">OptExtendedPreDistance_mm</a><small>(API)</small>：優化程序中的有切削區域等效計算的前長度。單位mm。</p>
</li>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPostDistance_mm">OptExtendedPostDistance_mm</a><small>(API)</small>：優化程序中的有切削區域等效計算的後長度。單位mm。</p>
</li>
</ul>
<figure>
  <img src="ExtendedDistance.png" alt="ExtendedDistance">
  <figcaption>優化程序中的有切削區域等效計算的長度</figcaption>
</figure>
<ul>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindlePowerSafetyFactor">OptSpindlePowerSafetyFactor</a><small>(API)</small>：主軸功率安全係數。</p>
<p>於優化後的NC碼，主軸功率將盡可能趨近於目標值。</p>
<p>設為0則忽略此限制。</p>
</li>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindleTorqueSafetyFactor">OptSpindleTorqueSafetyFactor</a><small>(API)</small>：主軸扭力安全係數。</p>
<p>優化後的NC碼，主軸扭力將盡可能趨近於目標值。</p>
<p>設為0則忽略此限制。</p>
</li>
<li><p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptThermalYieldSafetyFactor">OptThermalYieldSafetyFactor</a><small>(API)</small>：刀刃熱降伏安全係數。</p>
<p>刀刃表面溫度超過一定值時，刀刃表面會因溫差產生的壓力造成塑性變形。</p>
<p>優化後的NC碼，刀刃表面度將盡可能趨近於目標值。</p>
<p>設為0則忽略此限制。</p>
</li>
</ul>
<div class="NOTE">
<h5>Note</h5>
<p><strong>目標值算法</strong></p>
<p>目標值 = 100% / 安全係數</p>
</div>
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptPreferedForce_N">OptPreferedForce_N</a><small>(API)</small>：優化程中的目標力。單位N。
在符合所有限制的條件下，優化程序中的加工路徑產生的力會盡可能的趨近目標力。</li>
</ul>
<h2 id="限制的優先序">限制的優先序</h2>
<p>在有切削區域中：</p>
<p><strong>進給率的直接限制</strong> 優先於 <strong>加減速限制</strong>(<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxAcceleration_mmds2">OptMaxAcceleration_mmds2</a><small>(API)</small>) 優先於 <strong>基於物理特性算出的限制</strong>。</p>
<ul>
<li><p><strong>進給率的直接限制</strong> 包含</p>
<ul>
<li>腳本指令
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMinFeedrate_mmdmin">OptMinFeedrate_mmdmin</a><small>(API)</small></li>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxFeedrate_mmdmin">OptMaxFeedrate_mmdmin</a><small>(API)</small></li>
</ul>
</li>
<li>刀具設定
<ul>
<li>每刃最小進給</li>
<li>每刃最大進給</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>基於物理特性算出的進給率限制</strong> 包含</p>
<ul>
<li>腳本指令
<ul>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindlePowerSafetyFactor">OptSpindlePowerSafetyFactor</a><small>(API)</small></li>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindleTorqueSafetyFactor">OptSpindleTorqueSafetyFactor</a><small>(API)</small></li>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptThermalYieldSafetyFactor">OptThermalYieldSafetyFactor</a><small>(API)</small></li>
<li><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptPreferedForce_N">OptPreferedForce_N</a><small>(API)</small></li>
</ul>
</li>
<li>刀具設定
<ul>
<li>降伏力安全係數</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>若同級項目中的限制有衝突，則採取其中的最低進給率。</p>
<h2 id="nc碼中的優化腳本指令">NC碼中的優化腳本指令</h2>
<h3 id="於指定nc行不啟用優化">於指定NC行不啟用優化</h3>
<p>在執行 <a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_Preserve_">Preserve</a><small>(API)</small>() 的NC行中，不啟用優化。</p>
<div class="TIP">
<h5>Tip</h5>
<p><strong>指令範例</strong></p>
<p>在 <code>N0140</code> 行不啟用優化。</p>
<pre><code class="lang-nc">...
N0130 X-1.965 Y17.053
N0140 G03 X-2.66 Y38.193 I-103.796 J7.172 (;@Preserve();)
N0150 G01 X-3.068 Y40.776 
...
</code></pre>
</div>
<h3 id="於指定nc範圍不啟用優化">於指定NC範圍不啟用優化</h3>
<p>使用<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_BeginPreserve_">BeginPreserve</a><small>(API)</small>()的NC行及<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_EndPreserve_">EndPreserve</a><small>(API)</small>()的NC行及包含的NC範圍，不啟用優化。</p>
<div class="TIP">
<h5>Tip</h5>
<p><strong>指令範例</strong></p>
<p>在 <code>N0140</code>、<code>N0150</code>、<code>N0160</code> 行不啟用優化。</p>
<pre><code class="lang-nc">...
N0130 X-1.965 Y17.053
N0140 G03 X-2.66 Y38.193 I-103.796 J7.172 (;@BeginPreserve();)
N0150 G01 X-3.068 Y40.776 
N0160 X-3.555 Y43.338 (;@EndPreserve();)
N0170 X-4.125 Y45.875
...
</code></pre>
</div>
<h2 id="優化日誌">優化日誌</h2>
<p>優化過程提供兩種日誌功能，方便分析與除錯。</p>
<h3 id="優化過程日誌檔">優化過程日誌檔</h3>
<p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_EnableIndividualStepAdjustmentLog_">EnableIndividualStepAdjustmentLog</a><small>(API)</small>：啟用優化過程日誌檔輸出。預設為真。</p>
<p>啟用後，優化過程會為每個NC檔輸出一個 <code>.flatproc.log</code> 檔案，記錄各步階的優化條件經<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPreDistance_mm">OptExtendedPreDistance_mm</a><small>(API)</small>及<a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPostDistance_mm">OptExtendedPostDistance_mm</a><small>(API)</small>展開後獨立優化計算的資訊（不包含步階間的交互關係，如加減速）。</p>
<p>日誌檔案內容依步階順序輸出（即使平行計算時也會保持順序），每行包含：</p>
<ul>
<li>原始NC行位置與步階索引</li>
<li>各項優化限制的計算結果，例如：
<ul>
<li><code>FrtByPreferedForce_mm</code>: 依目標力計算的每刃進給</li>
<li><code>FrtByYieldingStressRatio_mm</code>: 依降伏應力比計算的每刃進給</li>
<li><code>FrtBySpindleTorqueRatio_mm</code>: 依主軸扭力比計算的每刃進給</li>
<li><code>FrtBySpindlePowerRatio_mm</code>: 依主軸功率比計算的每刃進給</li>
<li><code>FrtByThermalYieldingRatio_mm</code>: 依熱降伏比計算的每刃進給</li>
<li><code>FrtByReliefAngle_mm</code>: 依餘隙角碰撞計算的每刃進給</li>
</ul>
</li>
</ul>
<h3 id="嵌入式日誌註解">嵌入式日誌註解</h3>
<p><a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableEmbeddedLog_">OptEnableEmbeddedLog</a><small>(API)</small>：啟用嵌入式日誌註解。預設為真。</p>
<p>啟用後，優化後的NC檔中會在NC行後方加上 <code>(src(...))</code> 註解，包含：</p>
<ul>
<li><code>LineNo</code>: 原始NC檔的行號</li>
<li><code>StepIndex</code>: 對應的步階索引</li>
</ul>
<p>例如：<code>G01 X10.0 Y20.0 F500 (src(LineNo: 140, StepIndex: 256))</code></p>
<p>此功能方便對照原始NC碼與優化後的NC碼，並可追蹤每行NC碼對應的步階資訊。</p>
<div class="NOTE">
<h5>Note</h5>
<p><strong>指令格式</strong></p>
<pre><code class="lang-csharp">EnableIndividualStepAdjustmentLog = &lt;布林值&gt;;
OptEnableEmbeddedLog = &lt;布林值&gt;;
</code></pre>
</div>
<h2 id="輸出優化nc檔">輸出優化NC檔</h2>
<p>使用 <a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptimizeToFiles_">OptimizeToFiles</a><small>(API)</small> 方法輸出優化後的NC檔。</p>
<div class="NOTE">
<h5>Note</h5>
<p><strong>指令格式</strong></p>
<pre><code class="lang-csharp">OptimizeToFiles(&lt;路徑樣板&gt;);
</code></pre>
</div>
<p>&lt;路徑樣板&gt;支援 <a href="../Doc-Task/index.html#FileTemplate">檔案路徑樣板</a>。</p>
<div class="TIP">
<h5>Tip</h5>
<p><strong>指令範例</strong></p>
<pre><code class="lang-csharp">OptEnableFeedrate = true; 
OptEnableInterpolation = true; 
OptRapidFeed_mmdmin = 4000; 
OptMinFeedrate_mmdmin = 100; 
OptMaxFeedrate_mmdmin = 4000; 
OptMaxAcceleration_mmds2 = 10; 
OptExtendedPreDistance_mm = 3; 
OptExtendedPostDistance_mm = 2; 
OptSpindlePowerSafetyFactor = 1.5; 
OptSpindleTorqueSafetyFactor = 1.5;
OptThermalYieldSafetyFactor = 0;
OptPreferedForce_N = double.PositiveInfinity; 
PlayNcFile(&quot;NC/file1.nc&quot;);

OptRapidFeed_mmdmin = 8000; 
PlayNcFile(&quot;NC/file2.nc&quot;);

OptimizeToFiles(&quot;Cache/Opt-[NcName]&quot;);
</code></pre>
</div>
<p>組態設定可以穿插在NC仿真間，將會作用在組態設定後的NC仿真。</p>
<div class="WARNING">
<h5>Warning</h5>
<p>在 <a class="xref" href="../../../../../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_MachiningStepBuilt">MachiningStepBuilt</a><small>(API)</small> 事件內使用 <a class="xref" href="../../../../../api/Hi.MachiningSteps.MachiningStep.html#Hi_MachiningSteps_MachiningStep_UpdateNcOptOption_">UpdateNcOptOption</a><small>(API)</small> 時，不能與NC碼內嵌優化指令併用，否則可能因平行計算產生未定義行為。</p>
</div>
<p>以上範例為例，優化後的NC程式file1.nc的無切削區域快速進給為4000，file2.nc則為8000。</p>
<div class="TIP">
<h5>Tip</h5>
<p><strong>NC碼中的優化指令範例</strong></p>
<pre><code class="lang-nc">...
N0110 X-3.064 Y6.378 (;@OptMaxAcceleration_mmds2=10;)
N0120 X-2.411 Y11.712
N0130 X-1.965 Y17.053
N0140 G03 X-2.66 Y38.193 I-103.796 J7.172 (;@Preserve();)
N0150 G01 X-3.068 Y40.776 (;@OptMaxAcceleration_mmds2=100; OptMaxFeedrate_mmdmin=12000;)
N0160 X-3.555 Y43.338 
N0170 X-4.125 Y45.875
...
</code></pre>
<p>N0110至N0150前，優化區加速度設為10 <span class="math">\(mm/s^2\)</span>；
N0140單行不做優化；
N0150及以後，優化區加速度設為100 <span class="math">\(mm/s^2\)</span>；
N0150及以後，優化區切削區域最大進給設為12000 <span class="math">\(mm/min\)</span>；</p>
</div>

</article>

        <div class="contribution d-print-none">
        </div>

        <div class="next-article d-print-none border-top" id="nextArticle"></div>

      </div>

      <div class="affix">
        <nav id="affix"></nav>
      </div>
    </main>

    <div class="container-xxl search-results" id="search-results"></div>

    <footer class="border-top text-secondary">
      <div class="container-xxl">
        <div class="flex-fill">
          <span> Copyright © 2025 <a href='https://superhightech.com.tw'>Tech Coordinate</a>. All rights reserved. <a href='https://superhightech.com.tw'>超級高科技股份有限公司</a> © 2025 版權所有 </span>
        </div>
      </div>
    </footer>
  </body>
</html>
